package com.maoye.ossp.sys.log.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.statement.entity.BizStatementStmPrepaid;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.log.entity.SysLog;
import com.maoye.ossp.sys.log.entity.SysLogLogin;
import com.maoye.ossp.sys.log.service.ISysLogService;


/**
 * 日志Controller
 * 
 * @author chenc
 * @date 2014-01-15
 * @version 1.0
 */


@Controller
@RequestMapping(value = "/sys/log/")
public class SysLogController extends BaseController {

	@Resource
	private ISysLogService sysLogService;
	
	/**
	 * 查询系统日志列表
	 * @param log
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "list")
	public String logList(SysLog log, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, log);
		Page<SysLog> page = (Page<SysLog>) sysLogService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("SysLog", log);
		return "/sys/log/list";
	}

	/**
	 * 添加
	 * @param sysLog
	 * @param model
	 */
	@RequestMapping(value = "add")
	public String addLog(SysLog sysLog, Model model) {
		sysLog.setCreateBy("jonathan.zhuang");
		sysLog.setCreateTime(new Date());
		sysLog.setIp("127.0.0.1");
		sysLog.setMethod("post");
		model.addAttribute("sysLog", sysLog);
		return "/sys/log/add";
	}
	
	@RequestMapping(value = "save")
	public String logSave(SysLog sysLog, Model model,
			RedirectAttributes redirectAttributes) {
		if (sysLog.getId() == null || sysLog.getId() == 0) {
			sysLog.setId(idGenerator.getUId());
			sysLogService.create(sysLog);
		} 
		addMessage(redirectAttributes, "保存'" + sysLog.getType() + "'成功");
		return "redirect:/sys/log/list";
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) {
		SysLog sysLog =	sysLogService.getLog(id);
		model.addAttribute("sysLog", sysLog);
		return "/sys/log/add";
	}
	
	/**
	 * 根据ID查看单条日志记录
	 * @param id
	 * @param model
	 */
	@RequestMapping(value = "{id}/view")
	public String logView(@PathVariable String id, Model model) {
		SysLog sysLog = sysLogService.getLog(id);
		model.addAttribute("sysLog", sysLog);
		return "/sys/log/view";
	}
	
	/**
	 * 根据ID删除单条日志记录
	 * @param id
	 * @param model
	 */
	@RequestMapping(value = "{id}/delete")
	public String logDelete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
		sysLogService.expurgateLog(id);
		addMessage(redirectAttributes, "删除日志信息成功！");
		return "redirect:/sys/log/getByDateCondition";
	}
	
	/**
	 * 按月份批量删除
	 * @param request
	 * @param redirectAttributes
	 * @param model
	 */
	@RequestMapping(value = "expurgate")
	public String expurgate(HttpServletRequest request, RedirectAttributes redirectAttributes,
			 Model model){
		Map map = new HashMap();
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		String begin = "";
		String end ="";
		if(month == 12){ //如果选择的月份为12月
			begin = year+"-"+month+"-01";
			month = 1;
			year = year+1;
			end = year+"-0"+month+"-01";
		}
		else{ 
			Integer chooseMonth = month;
			if(chooseMonth.toString().length()<2){  //判断是否为1位数月份,是就追加0
				begin = year+"-0"+month+"-01";
				end = year+"-0"+(month+1)+"-01";
			}else{
				begin = year+"-"+month+"-01";
				end = year+"-"+(month+1)+"-01";
			}	
		}
		map.put("begin", begin);
		map.put("end", end);
		sysLogService.batchRemove(map);
		addMessage(redirectAttributes, "批量删除成功！");
		return "redirect:/sys/log/getByDateCondition";
	}
	
	/**
	 * 生成日期树
	 * @param response
	 */
	@RequestMapping(value = "dateTree")
	public void dateTree(HttpServletResponse response) throws IOException{
		Date currentDate = new Date(); 
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date = format.format(currentDate);
		String year = date.substring(0, date.indexOf("-"));
		List<String> lstTree = new ArrayList<String>();
		String dateParent = "{id:1, pId:0, name:\""+year+"年\" , open:true}";	
		lstTree.add(dateParent);
		for(int i=2;i<=13;i++){
			String month = "{id:"+i+", pId:1, name:\""+(i-1)+"月\" , open:false}";
			lstTree.add(month);
			for(int j = 300;j<=330;j++){
				String day = "{id:"+j+", pId:"+i+", name:\""+(j-299)+"日\" , open:false}";
				lstTree.add(day);
			}
		}
		JSONArray json = new JSONArray();
		String array = json.fromObject(lstTree).toString();
		PrintWriter out = response.getWriter();
		out.print(array);
		out.flush();
		out.close();
	}
	
	/**
	 * 根据日期节点获取日志
	 * @param request
	 * @param model
	 * @throws IOException 
	 */
	@RequestMapping(value = "getByDateCondition")
	public String getByDateCondition(SysLog log,HttpServletRequest request,Model model){
		Map map = new HashMap();
		String createTime = request.getParameter("date");
		String type = request.getParameter("type");
		QueryFilter queryFilter = getQuerFilter(request, log);
		if(createTime!=null){
			createTime = createTime.replaceAll("[\u4e00-\u9fa5]", "-");  //将所有汉字转换为"-"
			createTime = createTime.substring(0, createTime.lastIndexOf("-"));
			queryFilter
			.addFilter("createTime", createTime, QueryOP.LIKE);
		}	
		Page<SysLog> page = (Page<SysLog>)sysLogService.find(queryFilter);		
		model.addAttribute("page", page);
		model.addAttribute("sysLog", log);
		return "/sys/log/innerlist";
	}

}
